package com.liupaik.pennmarketplace;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;


public class BuySell extends Activity {
	 /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.buysell);
        
        Button toBuy = (Button) findViewById(R.id.buy_button);
        toBuy.setOnClickListener(new OnClickListener() {
        	public void onClick(final View view) {
        		
        		final ProgressDialog pdg = ProgressDialog.show(getDialogContext(), "", "Loading...", true);
				new Thread(){
					public void run() {
						try {
							Intent myIntent = new Intent(view.getContext(), TabMain.class);
			        		startActivityForResult(myIntent, 1);
						}
						catch (Exception e){
							Log.e("tag",e.getMessage());
						}
						pdg.dismiss();
					}
				}.start();
        	}
        });
        
        Button toSell = (Button) findViewById(R.id.sell_button);
        toSell.setOnClickListener(new OnClickListener() {
        	public void onClick(View view) {
        		Intent myIntent = new Intent(view.getContext(), Sell.class);
        		startActivityForResult(myIntent, 0);
        	}
        });
        
        
        
    }
    
	private Context getDialogContext() {
	    Context context;
	    if (getParent() != null) context = getParent();
	    else context = this;
	    return context;
	}
    
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	super.onCreateOptionsMenu(menu);
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.menu, menu);
    	return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	switch (item.getItemId()) {
		case R.id.preferences:
			startActivity(new Intent(this, Prefs.class));
			return true; 
			
		case R.id.logout:
			//IMPLEMENT LOGOUT HERE
			
			Toast.makeText(
					getApplicationContext(),
					"Successfully Logged Out!",
					Toast.LENGTH_SHORT)
					.show();
			Intent myIntent = new Intent(BuySell.this, Intro.class);
			startActivityForResult(myIntent, 0);
			
			
//			new AlertDialog.Builder(BuySell.this)
//			//.setTitle(Register.this.getResources().getString(R.string.register_alert))  
//			.setMessage("Successfully Logged Out!")  
//			//.setIcon(R.drawable.book)  
//			.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
//				public void onClick(DialogInterface dialog, int id) {
//					Intent myIntent = new Intent(BuySell.this, Intro.class);
//					startActivityForResult(myIntent, 0);
//				}
//			})
//			.show();
			
			return true;
    	}
    	return false;
    }
    
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            moveTaskToBack(true);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    
}
